package controllers

import (
	"github.com/gin-gonic/gin"
	"permange/models"
	"permange/pkg/app"
	"permange/pkg/e"
	"permange/services"
)

func Getemployee(c *gin.Context)  {
	err , data := services.Getemployee()
	if err != nil{
		app.Error(c, e.ERROR,err,err.Error())
	}
	app.OK(c, gin.H{"value": data},"查询成功")
}

func Getemployee_e(c *gin.Context)  {
	data := c.Query("eid")
	//fmt.Println(data)
	if data == ""{
		app.INFO(c,30001,"参数错误")
		return
	}
	employee,err :=services.Getemployee_e(data)
	if err != nil {
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, gin.H{"value":employee},"查询成功")
}

func Delemployee(c *gin.Context){
	data := c.Query("eid")
	err := services.Delemployee(data)
	err = c.ShouldBindJSON(&data)
	if err != nil{
		app.INFO(c,30001,"违反约束")
		return
	}
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, gin.H{"value":data},"删除成功")
}

func Updateemployee(c *gin.Context){
	var  data models.Employee
	err := c.ShouldBindJSON(&data)
	if err != nil {
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	err = services.Updateemployee(data)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, gin.H{"value":data},"更新成功！")
}

func Addemployee(c *gin.Context){
	var  data models.Employee
	err := c.ShouldBindJSON(&data)
	if err != nil {
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	err = services.Addemployee(data)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, gin.H{},"添加成功！")
}
